package cn.ycl.netty.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author YuanChangLiang
 * @Date 2021/1/8 9:59
 * 测试NIO
 */
public class NioFileChannel {
    public static void main(String[] args) throws IOException {
        String str = "hello,world, welcome:" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        //创建一个输出流->channel
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\all_file\\testChannel.txt");
        //通过fileOutputStream获取对应的FileChannel
        //这个fileChannel真实类型时FileChannelImpl
        FileChannel channel = fileOutputStream.getChannel();
        //创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入byteBuffer
        byteBuffer.put(str.getBytes());
        //对byteBuffer进行flip
        byteBuffer.flip();
        //将byteBuffer数据写入到fileChannel
        channel.write(byteBuffer);
        fileOutputStream.close();;


    }
}
